﻿using System.Threading.Tasks;
using CkSoftware.GroupMe.Sdk.Core;
using CkSoftware.GroupMe.Sdk.Core.Dao;
using CkSoftware.GroupMe.Sdk.Core.Exceptions;
using CkSoftware.GroupMe.Sdk.Helpers.Users.Commands;
using CkSoftware.GroupMe.Sdk.Helpers.Users.Dao;

namespace CkSoftware.GroupMe.Sdk.Helpers.Users
{
	/// <summary>
	/// Helper class for accessing the currently authenticated user data.
	/// </summary>
	public class UserHelper
	{
		private readonly IWebApiAccess _webApiAccess;

		/// <summary>
		/// Creates a new instance of the UserHelper.
		/// </summary>
		/// <param name="webApiAccess">The <see cref="IWebApiAccess" /> used to call the API.</param>
		public UserHelper(IWebApiAccess webApiAccess)
		{
			_webApiAccess = webApiAccess;
		}

		/// <summary>
		/// Gets the <see cref="UserInfo" /> data for the currently authenticated user.
		/// </summary>
		/// <returns>The UserInfo data for the currently authenticated user.</returns>
		public async Task<UserInfo> GetCurrentUser()
		{
			var command = new UsersMeCommand();
			GroupMeApiResponse<UserInfo> userInfoData = await _webApiAccess.ExecuteCommand(command);

			if (userInfoData.Meta.ParsedCode != GroupMeApiResultCode.Ok)
			{
				throw new GroupMeException(userInfoData.Meta);
			}

			return userInfoData.Response;
		}
	}
}